<!-- 这是活动票券页面 免费收费票 -->
<template>
  <div
    class="c-bg-sgray"
  >
    <div class="c-ph24 c-fs22 c-fc-gray c-pv20" v-if="vipTypeMap[vipType]">您的身份是{{vipTypeMap[vipType]}}</div>
    <div
      v-show="!noneShow"
      class="c-pb260"
    >
      <activity-bill-list
        v-if="ticketData.length"
        :ticketData="ticketData"
        :selectMtkType="selectMtkType"
        :selectId="selectId"
        :groupId="groupId"
        :vipType="vipType"
        :select="select"
        :recType="recType"
        @slectTicket="slectTicket"
      ></activity-bill-list>
    </div>
    <div
      class="certain-button c-bg-white c-flex-column c-br-tl20 c-br-tr20"
      v-show="!noneShow && ticketData.length > 0 && select != null"
    >
      <div class="c-w100 c-ph24 c-pv36 c-br-tl20 c-br-tr20 c-bd-t1">
        <div class="c-flex-row c-aligni-center c-justify-sb">
          <span class="c-fs26">选择数量</span>
          <shop-number
            :min='min'
            :max='max'
            :inputNum='defaultNum'
            :itemIndex="select"
            @subNum='subNum'
            @addNum='addNum'
            @inputChange='inputChange'
          ></shop-number>
        </div>
        <div class="c-fs20 c-fc-sgray c-mt30">注：{{min}}张起购，限购{{max}}张</div>
      </div>
      <div class="c-bd-t1 c-hh88 c-flex-row c-flex-center c-ph30 c-w100">
        <div
          class="c-fc-white c-w100 c-fs28 c-hh64 c-br36 theme-bg c-flex-row c-flex-center"
          @click="toBuyActivity()"
        >确定</div>
      </div>
    </div>
    <loading-status-tem
      :noData="noneShow"
      :noDataText="'无票'"
    ></loading-status-tem>
    <!-- 选择阶梯 -->
    <SelectStairCollage v-if="isShowStair" :isShowStair.sync="isShowStair" :collageObject="collageObject" @clickNext="toBuyActivity"/>
    <!-- 关注二维码 -->
    <offical-account
      :showAccountQcode="showAccountQcode"
      :desc="officalDesc"
      :imgSrc="$addXossFilter(mpQrcode)"
      @hideDialog="hideDialog"
    ></offical-account>
  </div>
</template>
<script>
import { utilJs } from "@/utils/common.js"
import loadingStatusTem from "@/components/templates/common/loadingStatusTem.vue";
import ShopNumber from "@/components/templates/shopNumber.vue";
import officalAccount from '@/components/templates/common/officalAccount';
import ActivityBillList from '@/components/activity/activityBillList';
import SelectStairCollage from "@/components/templates/common/selectStairCollage.vue";
export default {
  name: "ActivityBills",
  components: {
    loadingStatusTem,
    ShopNumber,
    officalAccount,
    ActivityBillList,
    SelectStairCollage
  },
  data() {
    return {
      theme: localStorage.getItem("colorName") ? localStorage.getItem("colorName") : 'mb5_default',
      select: null, // 选择的票de index
      ticketId: null, // 选择的票的id
      explain: '', // 票券说明
      noneShow: false,
      isFree: 0, // 是否免费 0 免费 1 收费
      vipType: 1, // 用户身份
      isAudit: 0, //是否要审核 0 不审核 1审核
      vipTypeMap: {
        1: '普通用户',
        2: 'VIP',
        3: JSON.parse(localStorage.getItem("customName")).agentName,
        4: 'SVIP',
        5: JSON.parse(localStorage.getItem("customName")).partnerName,
        6: JSON.parse(localStorage.getItem("customName")).branchName,
        7: JSON.parse(localStorage.getItem("customName")).cooName,
        8: JSON.parse(localStorage.getItem("customName")).agencyName,
      },
      ticketData: [],
      groupId: -1,
      collageId: -1,
      companyAuth: JSON.parse(localStorage.getItem("companyAuth")),
      defaultNum: 1,
      selectNum: 1,
      min: 1,
      max: 1,
      isShowStair: false,
      collageObject: null,
      choosePriceIndex: 0,
      showAccountQcode: false,
      officalDesc: '关注我们后才可发起砍价哦',
      mpQrcode: '',
      recType: 0, //推荐类型 1 访问 2 绑定 3 支付
    }
  },
  filters: {
    formatStringTime(value) {
      if (value != undefined && value != "" && value != null) {
        return value.substr(0, 10).replace(/-/g, '.');
      } else {
        return value;
      }
    },
  },
  created() {
    utilJs.appShare(this);
  },
  methods: {
    hideDialog() {
      this.showAccountQcode = false;
    },
    // 判断是否关注公众号
    checkUserIsFollowedMp(bargainId) {
      return new Promise(function (resolve, reject) {
        utilJs.getMethod(`${global.apiurl}bargains/getMpData?bargainId=${bargainId}`,
          res => {
            resolve(res);
          },
          failRes => {
            reject(failRes)
          }
        )
      })
    },
    //数量增加
    addNum(item) {
      this.selectNum = item.inputNumber;
    },
    //数量减少
    subNum(item) {
      this.selectNum = item.inputNumber;
    },
    //输入数量
    inputChange(item) {
      this.selectNum = item.inputNumber;
    },
    toBuyActivity(index = -1) {
      // 点击确定
      if (this.select == null) {
        return;
      }
      let selectItem = this.ticketData[this.select];
      let goPathQuery = {
        activityId: this.$route.query.activityId,
        ticketId: this.ticketId,
        selectNum: this.selectNum,
        recType: this.recType,
        recRelateId: this.$route.query.recRelateId
      };
      if (selectItem && selectItem.mtkType) {
        goPathQuery.mtkType = selectItem.mtkType; // 同个票券可能会参加营销活动，所以需要把营销类型也传过去判断是哪个票
      }
      if (this._goodsRoomId) {
        goPathQuery.goodsRoomId = this._goodsRoomId;
      }
      if (this._refereeId) {
        goPathQuery.refereeId = this._refereeId;
      }
      //判断是否是阶梯拼团 index < 0 说明不是阶梯拼团组件SelectStairCollage的回调
      if (this.isFree == 1 && (selectItem.mtkType == 2 || this.selectMtkType == 2) && selectItem.collage && selectItem.collage.status == 2 && selectItem.collage.collageId > 0) {
        if (index < 0 && selectItem.collage.collageGroupType == 2 && this.groupId == -1) {
          this.collageObject = selectItem;
          this.isShowStair = true;
          return;
        }
        goPathQuery.groupId = -1;
        if (this.groupId != -1 && this.selectId == selectItem.atId && this.selectMtkType == selectItem.mtkType) {
          goPathQuery.groupId = this.groupId;
        }
        goPathQuery.collageId = selectItem.collage.collageId;
        goPathQuery.selectId = this.selectId;
        goPathQuery.collagePriceIndex = index;
      }
      if ((selectItem.mtkType == 12 || this.selectMtkType == 12) && selectItem.bargain != null && (selectItem.bargain.status == 2 || (selectItem.bargain.isPreheat == 1 && selectItem.bargain.status == 1))) {
        if (selectItem.bargain.userType == 1 && this.isRepeatOrder == 0) {
          this.$showCjToast({
            text: '您当前有砍价活动正在进行中，由于该活动不可重复下单，现无法再次发起!',
            type: 'warn'
          });
          return;
        }
        this.checkUserIsFollowedMp(selectItem.bargain.bargainId).then(res => {
          if (res.threshold == 2 && res.isFollowedMp == 0) {
            this.mpQrcode = res.mpQrcode;
            this.showAccountQcode = true;
            return;
          }
        })
      }
      this.$routerGo(this, "replace", {
        path: `/activity/activityEnroll`,
        query: goPathQuery
      })
    },
    slectTicket(index) {
      // 点击票，选择票种
      let least = this.ticketData[index].least;
      let remain = this.ticketData[index].number1;
      if (remain > 0 && remain >= least) {
        this.select = index;
        this.ticketId = this.ticketData[index].atId;
        let most = this.ticketData[index].most;
        // 剩余票数小于最大限购数时最大值取剩余票数
        this.defaultNum = this.selectNum = this.min = least;
        this.max = remain <= most ? remain : most;
      }
      // 计算选择的票价是否免费
      let item = this.ticketData[this.select]
      if (item.permission == 19) {
        this.isFree = 0 // 免费
      } else if ((this.vipType == 2) && (item.price * (item.vipDiscount / 100) == 0)) {
        this.isFree = 0 // 免费
      } else if (this.vipType == 4 && item.svipDiscount == 0) {
        this.isFree = 0 // 免费
      } else if ((this.vipType == 3 || this.vipType >= 5) && item.commonProdInfo.agentDiscount == 0) {
        this.isFree = 0 // 免费
      } else {
        this.isFree = 1 // 收费
      }
      // 判断票是否需要审核
      if (this.ticketData[index].isAudit == 1) {
        this.isAudit = 1
      } else {
        this.isAudit = 0
      }
    },
    getActivityBills() {
      let $this = this
      utilJs.postMethod(global.apiurl + 'activities/mgticket', { activityId: this.$route.query.activityId, collageId: this.$route.query.collageId, groupId: this.$route.query.groupId }, function (res) {
        $this.vipType = res.retUserType;
        $this.companyAuth = JSON.parse(localStorage.getItem("companyAuth"));
        if (res.data.length == 0) {
          $this.ticketData = [];
          $this.noneShow = true;
        } else {
          $this.noneShow = false;
          $this.ticketData = res.data;
          // 寻找目标对象
          let selectIndex = $this.ticketData.findIndex((value) => {
            return (value.atId == $this.selectId) && (value.mtkType == $this.selectMtkType) && (value.number1 > 0) && (value.number1 >= value.least);
          })
          if (selectIndex == -1) {
            // 当未寻找到符合得目标对象时
            selectIndex = $this.ticketData.findIndex((value) => {
              return (value.number1 > 0) && (value.number1 >= value.least)
            })
          }
          if (selectIndex != -1) {
            $this.defaultNum = $this.selectNum = $this.ticketData[selectIndex].least;
            $this.select = selectIndex;
            $this.ticketId = $this.ticketData[selectIndex].atId;
            $this.ticketData[selectIndex].permission == 19 ? $this.isFree = 0 : $this.isFree = 1;
            if ($this.vipType == 2 && $this.ticketData[selectIndex].vipDiscount == 0) {
              $this.isFree = 0
            }
            if ($this.vipType == 4 && $this.ticketData[selectIndex].svipDiscount == 0) {
              $this.isFree = 0;
            }
            // 判断服务商免费
            if (($this.vipType == 3 || $this.vipType >= 5) && $this.ticketData[selectIndex].commonProdInfo.agentDiscount == 0) {
              $this.isFree = 0;
            }
            $this.isAudit = $this.ticketData[selectIndex].isAudit == 1 ? 1 : 0
            $this.min = $this.ticketData[selectIndex].least;
            $this.max = $this.ticketData[selectIndex].most < $this.ticketData[selectIndex].number1 ? $this.ticketData[selectIndex].most : $this.ticketData[selectIndex].number1;
          }
        }
        // 服务商名称自定义
        $this.vipTypeMap = utilJs.getCustNameMap();
      }, () => {
        $this.$routerGo($this, "replace", {
          path: `/activity/activityDetail`,
          query: {
            ActivityNo: $this.$route.query.activityId
          }
        });
      })
    },
    //分享
    wechatShare: function () {
      utilJs.wechatConfig("", "", "", "", function () { });
    },
    //手机端分享
    appShare: function () {
      utilJs.appShareTrue("", "", "", "");
    }
  },
  activated() {
    setDocumentTitle("票券");// eslint-disable-line
    this.goodsRoomId = this.$route.query.goodsRoomId || '';
    this.groupId = this.$route.query.groupId ? this.$route.query.groupId : -1;
    this.collageId = this.$route.query.collageId ? this.$route.query.collageId : -1;
    this.isRepeatOrder = this.$route.query.isRepeatOrder || 0; //是否支持多次下单
    this.selectId = this.$route.query.prodId || '';
    this.selectMtkType = this.$route.query.mtkType || '';
    this.recType = this.$route.query.recType || 0;
    this.showAccountQcode = false;
    this.officalDesc = '关注我们后才可发起砍价哦';
    this.mpQrcode = '';
    this.selectNum = 1;
    this.select = null;
    this.min = 1;
    this.max = 1;
    this.showType = 1;
    this.isShowStair = false;
    this.collageObject = null;
    this.choosePriceIndex = 0;
    this.getActivityBills();
    this.wechatShare();
  },
  deactivated() {
    for (let i in this.ticketData) {
      if (this.ticketData[i].number1 > 0) {
        this.select = i
        this.ticketId = this.ticketData[i].atId
        this.ticketData[i].permission == 19 ? this.isFree = 0 : this.isFree = 1;
        if (this.vipType == 2 && this.ticketData[i].vipDiscount == 0) {
          this.isFree = 0;
        }
        if (this.vipType >= 3 && this.ticketData[i].svipDiscount == 0) {
          this.isFree = 0;
        }
        this.isAudit = this.ticketData[i].isAudit == 1 ? 1 : 0
        break
      }
    }
  }
}
</script>
<style scoped>
.c-bg-ticket {
  width: 100%;
  height: 100%;
}
.c-zindex1 {
  z-index: 1;
}
.certain-button {
  position: fixed;
  bottom: 0;
  z-index: 11;
  max-width: 640px;
  width: 100%;
  display: flex;
  display: -webkit-flex;
  align-items: center;
  -webkit-align-items: center;
}
.c-pb260 {
  padding-bottom: 6.5rem;
}
.vux-popup-dialog {
  background-color: #ffffff !important;
}
</style>


